package consumer_manager

import (
	"net"
	"os"
	"strconv"
	"strings"
)

// getDefaultConsumerName 获取默认消费者名称
func getDefaultConsumerName() string {
	if name := os.Getenv("SCALING_CONSUMER_NAME"); name != "" {
		return name
	}
	return "default_consumer"
}

// getDefaultConsumerRemark 获取默认消费者备注
func getDefaultConsumerRemark() string {
	if remark := os.Getenv("SCALING_CONSUMER_REMARK"); remark != "" {
		return remark
	}
	return ""
}

// getHostIP 获取宿主机内网IP
func getHostIP() string {
	if ip := os.Getenv("SCALING_HOST_IP"); ip != "" {
		return ip
	}

	// 自动获取内网IP
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return "127.0.0.1"
	}

	for _, addr := range addrs {
		if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
			if ipNet.IP.To4() != nil {
				ip := ipNet.IP.String()
				// 优先返回内网IP段
				if strings.HasPrefix(ip, "192.168.") ||
					strings.HasPrefix(ip, "10.") ||
					strings.HasPrefix(ip, "172.") {
					return ip
				}
			}
		}
	}

	return "127.0.0.1"
}

// getHostPort 获取宿主机映射端口
func getHostPort() string {
	if port := os.Getenv("SCALING_HOST_PORT"); port != "" {
		return port
	}
	return "18888"
}

// getNSQLookupHost 获取NSQ Lookup地址
func getNSQLookupHost() string {
	if host := os.Getenv("SCALING_NSQ_LOOKUP_HOST"); host != "" {
		return host
	}
	return "127.0.0.1:4161"
}

// getNSQPubMsgHost 获取NSQ发布消息地址
func getNSQPubMsgHost() string {
	if host := os.Getenv("SCALING_NSQ_PUB_MSG_HOST"); host != "" {
		return host
	}
	return "127.0.0.1:4150"
}

// getNSQHTTPAddress 获取NSQ HTTP地址
func getNSQHTTPAddress() string {
	if addr := os.Getenv("SCALING_NSQ_HTTP_ADDRESS"); addr != "" {
		return addr
	}
	return "127.0.0.1:4151"
}

// getNSQAdminAddress 获取NSQ Admin地址
func getNSQAdminAddress() string {
	if addr := os.Getenv("SCALING_NSQ_ADMIN_ADDRESS"); addr != "" {
		return addr
	}
	return "127.0.0.1:4171"
}

// getDefaultTopic 获取默认Topic
func getDefaultTopic() string {
	if topic := os.Getenv("SCALING_NSQ_TOPIC"); topic != "" {
		return topic
	}
	return "default_topic"
}

// getDefaultChannel 获取默认Channel
func getDefaultChannel() string {
	if channel := os.Getenv("SCALING_NSQ_CHANNEL"); channel != "" {
		return channel
	}
	return "default_channel"
}

// getAPIPort 获取API端口
func getAPIPort() string {
	if port := os.Getenv("SCALING_API_PORT"); port != "" {
		return port
	}
	return "18888"
}

// getMaxConsumers 获取最大消费者数量
func getMaxConsumers() int {
	if maxStr := os.Getenv("SCALING_MAX_CONSUMERS"); maxStr != "" {
		if max, err := strconv.Atoi(maxStr); err == nil && max > 0 {
			return max
		}
	}
	return 10
}

// getSpawnConsumerCount 获取启动消费者数量
func getSpawnConsumerCount() int {
	if spawnStr := os.Getenv("SCALING_SPAWN_CONSUMER_COUNT"); spawnStr != "" {
		if spawn, err := strconv.Atoi(spawnStr); err == nil && spawn >= 0 {
			return spawn
		}
	}
	return 1
}

// getDepthLimit 获取消息积压报警阈值
func getDepthLimit() int {
	if depthStr := os.Getenv("SCALING_DEPTH_LIMIT"); depthStr != "" {
		if depth, err := strconv.Atoi(depthStr); err == nil && depth >= 0 {
			return depth
		}
	}
	return 1000
}
